package com.lixin.smartclassroom.ui.home.tab4.activity

import android.graphics.Color
import com.drake.channel.receiveEvent
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.api.coursedagang
import com.lixin.smartclassroom.api.yanxuecoursedetail
import com.lixin.smartclassroom.api.zhcoursedetail
import com.lixin.smartclassroom.bean.BaseBean
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.databinding.ActivitySmartCourseDetailBinding
import com.lixin.smartclassroom.databinding.ActivityYanxueDetailBinding
import com.lixin.smartclassroom.ext.child_id
import com.lixin.smartclassroom.ext.user_role
import com.lixin.smartclassroom.ui.home.adapter.courseChapter
import com.lixin.smartclassroom.ui.home.adapter.oneText
import com.lixin.smartclassroom.ui.home.tab2.activity.AddChapterAct
import com.lixin.smartclassroom.ui.home.tab2.activity.AddCourseAct
import com.lixin.smartclassroom.ui.home.tab2.activity.AllGroupAct
import com.lixin.smartclassroom.ui.home.tab2.activity.CoursePermissionAct
import com.lixin.smartclassroom.ui.home.tab2.activity.CourseReviewAct
import com.lixin.smartclassroom.ui.home.tab5.activity.QuestionnaireDetailAct
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.dp2px
import com.lixinkeji.extension.getIntentString
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.load
import com.lixinkeji.extension.loadCircle
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.setRoundRectBg
import com.lixinkeji.extension.start
import com.lixinkeji.extension.toast
import com.lixinkeji.extension.visible
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description 研学详情
 * Author: lwc
 * Date:2023/12/14 8:38
 */
class YanXueDetailAct : BaseActivity<ActivityYanxueDetailBinding>() {
    override fun getViewBinding() = ActivityYanxueDetailBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    private lateinit var listBean: ArrayList<DataListBean>
    override fun title() = "详情"
    private var taskType = "3"
    private var baseBean: BaseBean? = null
    private var courseid = ""
    private var courseState = "" //1未发布   2已发布


    override fun iniView() {
        taskType = getIntentString("type")
        courseid = getIntentString("courseid")

        getCourseData()

        binding.apply {
            tvSubject.setRoundRectBg(Color.parseColor("#009B93"), dp2px(5f).toInt())

            when (user_role) {
                1 -> {
                    btnAttend.visible()
                    llYanxue.visible()
                    setStarCount(4)
                    tvFinishState.text = "TA已完成"

                }

                2 -> {
                    btnAttend.visible()
                    llYanxue.visible()
                    setStarCount(3)
                    tvFinishState.text = "你已完成"
                }

                3 -> {
                    btnAttend.gone()
                    llStudent.gone()
                    llTeacher.visible()
                    llContent.gone()
                    llYanxue.visible()
                    tvDate.visible()
                    tvLevel.visible()
                    ratingBar.visible()
                    tvFinishState.text = "你已完成"
                }
            }



            smartRefreshLayout.setOnRefreshListener {
                getCourseData()
                getCourseChapterData()
            }
            smartRefreshLayout.setEnableLoadMore(false)


            receiveEvent<String>("refreshDaGang") {
                getCourseData()
                getCourseChapterData()
            }

            receiveEvent<String>("finishAct") {
                getCourseData()
            }
        }
    }

    private fun getCourseData() {
        binding.apply {
            scopeNetLife {
                baseBean = when (user_role) {
                    1 -> yanxuecoursedetail(courseid, child_id)
                    else -> yanxuecoursedetail(courseid)
                }
                if (baseBean!!.result == "0") {
                    rcvLabel.oneText(1).models = baseBean!!.biaoqian

                    courseState = baseBean!!.state.toString()
                    img.load(baseBean!!.image!!)
                    tvSubject.text = baseBean!!.lablename
                    tvTitle.text = baseBean!!.title
                    tvTime.text = "开始时间：" + baseBean!!.begintime?.subSequence(0, 16)
                    tvAttendCount.text = baseBean!!.studentcount + "人参与"
                    tvTeacherName.text = baseBean!!.teachername
                    imgTeacher.loadCircle(baseBean!!.teacherimage!!)
                    tvLevel.text = baseBean!!.nandu
                    tvFinishDay.text = baseBean!!.wcday + "天"

                    if (baseBean!!.day.isNullOrEmpty()) {
                        tvDay.text = "未开始"
                        tvDate.text = "未开始"
                    } else {
                        tvDay.text = baseBean!!.day
                        tvDate.text = baseBean!!.day
                    }


                    tvNanduStudent.text = "难度：" + baseBean!!.nandu

                    if (baseBean!!.ifbaoming == "2") {
                        btnAttend.gone()
                        tvEvaluateTeacher.visible()
                    } else {
                        if (user_role != 3)
                            btnAttend.visible()
                    }

                    when (user_role) {
                        1, 2 -> {
                            when (baseBean!!.baomingzhuangtai) {
                                "3", "" -> btnAttend.visible()
                                else -> {
                                    btnAttend.gone()
                                    tvEvaluateTeacher.visible()
                                }
                            }
                        }

                        3 -> {
                            when (baseBean!!.ifwon) {
                                "2" -> {
                                    btnAttend.gone()
                                    llYanxue.visible()
                                    setStarCount(3)
                                    tvFinishState.text = "你已完成"
                                    llStudent.visible()
                                    llContent.gone()
                                    llTeacher.gone()
                                    tvDate.gone()
                                    tvLevel.gone()
                                    ratingBar.gone()

                                }

                                else -> {

                                }

                            }
                        }
                    }



                    when (baseBean!!.nandu) {
                        "简单" -> {
                            ratingBar.setStar(1f)
                            ratingBarStudent.setStar(1f)
                        }

                        "中等" -> {
                            ratingBar.setStar(3f)
                            ratingBarStudent.setStar(3f)
                        }

                        "困难" -> {
                            ratingBar.setStar(5f)
                            ratingBarStudent.setStar(5f)
                        }
                    }


                    getCourseChapterData()

                    if (baseBean!!.state == "1") {
                        btnEdit.visible()
                        btnPermission.visible()
                        btnAddChapter.visible()
                    } else {
                        btnReview.visible()
                        btnYanXueGroup.visible()
                        btnAllGroup.visible()
                        btnPermission.visible()
                    }

                }
            }
        }

    }

    private fun getCourseChapterData() {
        binding.apply {
            scopeNetLife {
                listBean = when (user_role) {
                    1 -> coursedagang(courseid, taskType, child_id)!!
                    else -> coursedagang(courseid, taskType)!!
                }
                recycler.courseChapter(
                    taskType,
                    courseState,
                    baseBean?.baomingzhuangtai,
                    baseBean?.ifwon
                ).models =
                    listBean

                if (smartRefreshLayout.isRefreshing)
                    smartRefreshLayout.finishRefresh()
            }
        }
    }


    /**
     *@Description 设置星星个数
     *@date 创建时间2023/12/19
     *
     */
    private fun setStarCount(count: Int) {
        binding.apply {
            if (count > 0) {
                val layoutParams = ratingBarStudent.layoutParams
                ratingBarStudent.setStar(count.toFloat())
                layoutParams.width = dp2px(count * 10 + 5 * (count - 1).toFloat()).toInt()
                ratingBarStudent.layoutParams = layoutParams
                ratingBarStudent.isClickable = false
            } else {
                toast("研学难度不能为0")
            }


        }
    }


    override fun onClick() {
        binding.apply {
            tvEvaluateTeacher.onClick {
                start(QuestionnaireDetailAct::class.java) {
                    putExtra("type", 2)
                    putExtra("teacherid",baseBean?.teacherId)
                }
            }

            btnAllGroup.onClick {
                start(AllGroupAct::class.java) {
                    putExtra("courseid", courseid)
                }
            }

            btnEdit.onClick {
                start(AddCourseAct::class.java) {
                    putExtra("type", taskType)
                    putExtra("isEdit", "1")
                    putExtra("courseid", courseid)
                }
            }

            btnAddChapter.onClick {
                start(AddChapterAct::class.java) {
                    putExtra("type", taskType)
                    putExtra("courseid", courseid)
                }
            }
            btnReview.onClick {
                start(CourseReviewAct::class.java) {
                    putExtra("courseid", courseid)
                }
            }
            btnYanXueGroup.onClick {
                start(SetGroupAct::class.java) {
                    putExtra("courseid", courseid)
                }
            }
            btnAttend.onClick {
                start(EventRegistrationAct::class.java) {
                    putExtra("courseid", courseid)
                }
            }
            btnPermission.onClick {
                start(CoursePermissionAct::class.java) {
                    putExtra("courseid", courseid)
                    putExtra("type", taskType)
                    putExtra("quanxian", baseBean!!.quanxian)
                    putExtra("customerids", baseBean!!.customerids)
                }
            }
        }
    }

}